<script setup>
import {ref} from 'vue'
import CategoryPanel from './components/CategoryPanel.vue'
import CustomNavbar from './components/CustomNavbar.vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '../../services/home';
import { onLoad } from '@dcloudio/uni-app'
import { useGuessList } from '../../composables/index'
// ===================轮播图=======================
// 定义轮播图数据变量
const bannerList = ref([])
// 定义获取轮播图数据方法
const getHomeBannerData = async () => {
  // 发请求
  const { result } = await getHomeBannerAPI()
  // 赋值
  bannerList.value = result
}
// =======================分类数据=====================
const categoryList = ref([])
// 获取数据方法
const getHomeCategoryData = async () => {
  const { result } = await getHomeCategoryAPI()
  categoryList.value = result
}
// ========================热门数据==================
const hotList = ref([])
const getHomeHotData = async () => {
  const { result } = await getHomeHotAPI()
  hotList.value = result
}
// ======================猜你喜欢==================
const { guessRef, onScrolltolower } = useGuessList()
// ==========初始化调用================
// 注意：页面生命周期用小程序那套，组件生命周期用vue那套
// onLoad(() => {
//   getHomeBannerData()
//   getHomeCategoryData()
//   getHomeHotData()
// })
// ==========下拉刷新================
const isTriggered = ref(false)
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢组件的数据
  guessRef.value.resetData()
  // 加载数据
  await Promise.all([
    getHomeBannerData(), 
    getHomeCategoryData(), 
    getHomeHotData(),
    guessRef.value.getMore()
  ])
  // 关闭动画
  isTriggered.value = false
}
// 加载中标记
const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([
    getHomeBannerData(), 
    getHomeCategoryData(), 
    getHomeHotData(),
  ])
  isLoading.value = false
  console.log(isLoading.value)
})
</script>
<template>
  <PageSkeleton v-if="isLoading"></PageSkeleton>
  <template v-else>
    <!-- 自定义头部组件 -->
    <CustomNavbar></CustomNavbar>
    <scroll-view refresher-enabled :refresher-triggered="isTriggered" @refresherrefresh="onRefresherrefresh" scroll-y class="scroll-view" @scrolltolower="onScrolltolower">
      <!-- 自定义轮播图 -->
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <!-- 分类组件 -->
      <CategoryPanel :list="categoryList"></CategoryPanel>
      <!-- 热门组件 -->
      <HotPanel :list="hotList"></HotPanel>
      <!-- 猜你喜欢组件 -->
      <XtxGuess ref="guessRef"></XtxGuess>
    </scroll-view>
  </template>
  </template>
<style lang="scss">
// 注意点：组件里面要写page样式，那么style标签不能写scoped属性，否则不生效
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1
}
</style>
